package com.visionblue.business.ship.page.outer

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.visionblue.business.ship.R
import com.visionblue.business.ship.base.BaseFragment
import com.visionblue.business.ship.base.DefaultObserver
import com.visionblue.business.ship.model.bean.response.OrdersApplyInfo
import com.visionblue.business.ship.page.orders.OrderApplyDetailActivity
import com.visionblue.business.ship.page.orders.OrderViewModel

import com.visionblue.business.ship.util.toast
import kotlinx.android.synthetic.main.dialog_selection.*
import kotlinx.android.synthetic.main.frag_orders_apply.*
import kotlinx.android.synthetic.main.frag_orders_receive.*

import org.koin.androidx.viewmodel.ext.android.sharedViewModel

/**
 * des:申报单
 * author: ysj
 * date: 2021/3/29
 */
open class TransCompanyFragment: BaseFragment(),View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {

    private lateinit var viewEmpty: View
    private var mPageNum: Int = 1
    private var isRefresh: Boolean = true
    private lateinit var  mAdapterApply:PortStoreListAdapter
    private val mList = ArrayList<OrdersApplyInfo>()

    protected val mViewModel: OrderViewModel by sharedViewModel()

    override val layoutId: Int
        get() = R.layout.frag_orders_apply

    override fun initView(savedInstanceState: Bundle?) {
        initRefresh()

        mAdapterApply.onItemChildClickListener =this
    }

    private fun initRefresh() {
        viewEmpty = LayoutInflater.from(activity).inflate(R.layout.view_empty,null)
        viewEmpty.setOnClickListener(this)

        mAdapterApply = PortStoreListAdapter(R.layout.item_apply_order)
        mAdapterApply.emptyView = viewEmpty

        rv_apply.layoutManager = LinearLayoutManager(context)
        rv_apply.adapter = mAdapterApply


        srl_apply?.setDisableContentWhenRefresh(true)
        srl_apply?.setDisableContentWhenLoading(true)
        srl_apply?.autoRefresh()
        srl_apply?.setEnableLoadMore(true)

        srl_apply?.setOnRefreshLoadMoreListener(object: OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mPageNum++
                isRefresh = false
                getListData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                mPageNum = 1
                isRefresh = true
                getListData()
            }

        })

        data()
    }

    private fun data() {
        mViewModel.orderApplyListResult.observe(this, DefaultObserver(action = {
            srl_apply?.finishRefresh()
            srl_apply?.finishLoadMore()
            if (it.data!=null){
                val content = it.data?.content?:return@DefaultObserver
                if(isRefresh){
                    mList.clear()
                    if(content!=null&& content.isNotEmpty()){
                        mList.addAll(content)
                    }
                    //mAdapterApply.replaceData(mList)
                }else{
                    mList.addAll(content)
                   // mAdapterApply.addData(content)
                }

                if(content.size<10){
                    srl_apply?.setNoMoreData(true)
                }
            }
        }))
    }

    override fun onClick(v: View?) {
        when(v){
            viewEmpty->{
                getListData()
            }
        }
    }

    private fun getListData() {
        mViewModel.getOrdersApplyList(mPageNum)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val data = adapter?.data?.get(position) as OrdersApplyInfo
        when(view?.id){
            R.id.ll_btn_check ->{
                OrderApplyDetailActivity.start(activity!!,data.id)
            }
        }
    }
}